﻿@page "/Admin/NoticeReceiveds"
@attribute [Authorize]
@attribute [TabItemOption(Text ="已收公告",Icon ="fa fa-th")]
@inherits BasePage
@inject ISysNoticeService SysNoticeService
@inject ISysUserService SysUserService

<div class="overflow-hidden h-100">
    <Table TItem="SysNotice"
            @ref = "@_table"
            IsPagination="true"
            IsStriped="true"
            IsMultipleSelect="true"
            IsBordered="true"
            IsFixedHeader="true" 
            AllowResizing="true"
            ShowLoading="true"
            ShowColumnList="true"
            ShowCardView="true"
            ShowSearch="true"
            ShowEmpty="true" 
            ShowToolbar="true"
            ShowDefaultButtons="false"
            ShowExtendButtons ="true"
            SearchMode="SearchMode.Top"
            SearchModel="@_search"
            AddModalTitle="新增通知公告-草稿"
            ExtendButtonColumnWidth="65"
            PageItemsSource="@(new int[] { 15, 50 })"
            OnQueryAsync="@OnQueryAsync">
        <TableColumns>
            <TableColumn @bind-Field="@context.Title" />
            <TableColumn @bind-Field="@context.Type" />
            <TableColumn @bind-Field="@context.Status" />
        </TableColumns>
        <DetailRowTemplate>
            <div class="container">
              <figure>
                  <blockquote class="blockquote">
                    <p>@context.Title</p>
                  </blockquote>
                  <figcaption class="blockquote-footer">
                    @context.PublicTime <cite title="@context.PublicUserName">@context.PublicUserName</cite>
                  </figcaption>
                </figure>
                @(new MarkupString($"<p>{context.Content}</p>"))
            </div>
        </DetailRowTemplate>
        <RowButtonTemplate>
            @if (context.ReadStatus == NoticeUserStatus.UNREAD)
            { 
                <Button Size="Size.ExtraSmall" OnClick="() => OnUpdateAsync(context, NoticeStatus.PUBLIC)" Icon="fa fa-edit" Text="标记已读" />  
            }
        </RowButtonTemplate>
        <SearchTemplate>
            <GroupBox Title="搜索条件">
                <div class="row g-3 form-inline">
                    <div class="col-sm-3">
                        <BootstrapInput @bind-Value="@context.SearchValue" ShowLabel="true"/>
                    </div>
                    <div class="col-sm-3">
                        <RadioList @bind-Value="@context.Type" ShowLabel="true"/>
                    </div>
                </div>
            </GroupBox>
        </SearchTemplate>
    </Table>
</div>

@code{
    private Table<SysNotice>? _table;
    private SysNotice _search = new();
    /// <summary>
    /// 更新、删除、撤回操作
    /// </summary>
    /// <param name="item"></param>
    /// <param name="status"></param>
    /// <returns></returns>
    private async Task OnUpdateAsync(SysNotice item, NoticeStatus status)
    {
        await _table!.ToggleLoading(true);
        item.Status = status;
        if (await SysNoticeService.ChangeStatusAsync(item))
        {
            _table!.SelectedRows.Clear();
            await _table!.QueryAsync();
        }
        await _table!.ToggleLoading(false);
    }

    private Task<QueryData<SysNotice>> OnQueryAsync(QueryPageOptions options)
        => SysNoticeService.ReceivedAsync(_search.Adapt<NoticePageInput>(options));
}